//
//  AudioChoiceView.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/13.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "AudioChoiceView.h"

@implementation AudioChoiceView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
     self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"资讯", @"娱乐", @"音乐", @"历史", @"情感", @"电台"]];
    self.segmentedControl.frame = CGRectMake(0, 0, ScreenFram.width, 30);
    [self.segmentedControl setSelectionIndicatorHeight:4.0f];
    [self.segmentedControl setBackgroundColor:[UIColor colorWithRed:167/255.0 green:194/255.0 blue:206/255.0 alpha:1.0]];
    [self.segmentedControl setTextColor:[UIColor blackColor]];
    [self.segmentedControl setSelectionIndicatorColor:[UIColor cyanColor]];
    [self.segmentedControl setSelectionIndicatorMode:HMSelectionIndicatorFillsSegment];
    [self.segmentedControl setSegmentEdgeInset:UIEdgeInsetsMake(0, 6, 0, 6)];
    [self.segmentedControl setCenter:CGPointMake(ScreenFram.width/2.0, 20)];
    [self.segmentedControl addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.segmentedControl];
    
    CGFloat position = ScreenFram.width / 12;
    
    for (NSInteger i = 0; i < 6; i++) {
        
        UIImageView *imageViewButton = [[UIImageView alloc] initWithFrame:CGRectMake(position + (i % 3)*ScreenFram.width/3.0, (i / 3) *(position+20)*2 + 50, position*2, position*2)];
        imageViewButton.tag = 10000+i;
        imageViewButton.userInteractionEnabled = YES;
        imageViewButton.layer.masksToBounds = YES;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(position/2 + (i % 3)*ScreenFram.width/3.0, (i / 3) *(position+20)*2 + position*2 + 55, position*3, 25)];
        titleLabel.tag = 100 + i;
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        playButton.frame = CGRectMake(position-16, position-16, 32, 32);
        playButton.tag = 1000+i;
        [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
        [playButton setBackgroundImage:[UIImage imageNamed:@"play_32.png"] forState:UIControlStateNormal];
        [imageViewButton addSubview:playButton];
        [self addSubview:imageViewButton];
        
        imageViewButton.layer.cornerRadius = position;
        playButton.layer.cornerRadius = 16;
        
        [imageViewButton release];
        [titleLabel release];
    }
    
}

- (void)segmentAction
{
    [self setMyImage];
    if (self.segmentKey != self.segmentedControl.selectedIndex && self.key > 0) {
        [((UIButton *)[self viewWithTag:self.key]) setBackgroundImage:[UIImage imageNamed:@"play_32.png"] forState:UIControlStateNormal];
    } else if ([[AudioPlayerHandle shareHandle].audioStream isPlaying] && self.key > 0) {
        [((UIButton *)[self viewWithTag:self.key]) setBackgroundImage:[UIImage imageNamed:@"pause_32.png"] forState:UIControlStateNormal];
    }
    
}

- (void)setMyImage
{
    
    if (self.segmentedControl.selectedIndex == 0) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"news"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
    if (self.segmentedControl.selectedIndex == 1) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"entertainment"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
    if (self.segmentedControl.selectedIndex == 2) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"music"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
    if (self.segmentedControl.selectedIndex == 3) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"culture"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
    if (self.segmentedControl.selectedIndex == 4) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"emotion"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
    if (self.segmentedControl.selectedIndex == 5) {
        NSMutableArray *newsArray = [self.soundsDic objectForKey:@"radio"];
        for (NSInteger i = 0; i <= 5; i++) {
            soundsModel *soundModel = [newsArray objectAtIndex:i];
            [((UIImageView *)[self viewWithTag:10000+i]) sd_setImageWithURL:[NSURL URLWithString:soundModel.coverWebSmall] placeholderImage:nil];
            ((UILabel *)[self viewWithTag:100+i]).text = soundModel.title;
        }
    }
}

- (void)playAction:(UIButton *)button
{
    if (self.key != button.tag || self.segmentKey != self.segmentedControl.selectedIndex) {
        
        if(self.key > 0) {
            [((UIButton *)[self viewWithTag:self.key]) setBackgroundImage:[UIImage imageNamed:@"play_32.png"] forState:UIControlStateNormal];
        }
        
        self.key = button.tag;
        
        if (self.segmentedControl.selectedIndex == 0) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"news"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 0;
        }
        if (self.segmentedControl.selectedIndex == 1) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"entertainment"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 1;
        }
        if (self.segmentedControl.selectedIndex == 2) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"music"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 2;
        }
        if (self.segmentedControl.selectedIndex == 3) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"culture"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 3;
        }
        if (self.segmentedControl.selectedIndex == 4) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"emotion"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 4;
        }
        if (self.segmentedControl.selectedIndex == 5) {
            NSMutableArray *newsArray = [self.soundsDic objectForKey:@"radio"];
            soundsModel *soundModel = [newsArray objectAtIndex:button.tag%1000];
            [[AudioPlayerHandle shareHandle] createStreamer:soundModel.playPath64 duration:0];
            self.segmentKey = 5;
        }
        
        [[AudioPlayerHandle shareHandle].audioStream start];
        [button setBackgroundImage:[UIImage imageNamed:@"pause_32.png"] forState:UIControlStateNormal];
        
    } else {
        if ([[AudioPlayerHandle shareHandle].audioStream isPlaying]) {
            [[AudioPlayerHandle shareHandle].audioStream pause];
            
            [button setBackgroundImage:[UIImage imageNamed:@"play_32.png"] forState:UIControlStateNormal];
            
        } else if ([[AudioPlayerHandle shareHandle].audioStream isPaused]) {
            [[AudioPlayerHandle shareHandle].audioStream start];
            
            [button setBackgroundImage:[UIImage imageNamed:@"pause_32.png"] forState:UIControlStateNormal];
        }
    }
}



@end
